home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Word8Vector.sml < prev   
Encoding:
Text File  |  1997-08-18  |  3.9 KB  |  123 lines  |  [TEXT/Moml]

  1. (* Word8Vector.mlp *)
  2.  
  3. prim_eqtype vector;
  4. type elem = Word8.word;
  5.  
  6. local 
  7.     prim_val vector_ : int -> vector                 = 1 "create_string";
  8.     prim_val sub_    : vector -> int -> elem         = 2 "get_nth_char";
  9.     prim_val update_ : vector -> int -> elem -> unit = 3 "set_nth_char";
  10.     prim_val blit_   : vector -> int -> vector -> int -> int -> unit 
  11.                                                      = 5 "blit_string";
  12. in 
  13.  
  14. prim_val length : vector -> int = 1 "string_length";
  15.  
  16. val maxLen = 16777211;                  (* = (2^22-1)*4-1, with 32 bit *)
  17.  
  18. fun fromList (vs : elem list) =
  19.   let val n = List.length vs
  20.       val a = if n > maxLen then raise Size else vector_ n
  21.       fun init [] i = ()
  22.         | init (v::vs) i = (update_ a i v; init vs (i+1))
  23.   in (init vs 0; a) end;
  24.  
  25. fun tabulate(n, f : int -> elem) =
  26.   if n < 0 orelse n > maxLen then raise Size else
  27.   let val a = vector_ n
  28.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  29.   in (init 0; a) end;
  30.  
  31. fun sub(v, i) =
  32.   if i < 0 orelse i >= length v then raise Subscript 
  33.   else sub_ v i;
  34.  
  35. fun extract (vec, i, slicelen) = 
  36.     let val n = case slicelen of NONE => length vec - i | SOME n => n
  37.         val newvec = if i<0 orelse n<0 orelse i+n > length vec then
  38.                          raise Subscript
  39.                      else
  40.                          vector_ n
  41.     in blit_ vec i newvec 0 n; newvec end;
  42.  
  43. fun concat vecs =
  44.     let fun acc [] len       = len
  45.           | acc (v1::vr) len = acc vr (length v1 + len)
  46.         val len = acc vecs 0
  47.         val newvec = if len > maxLen then raise Size else vector_ len 
  48.         fun copyall to []       = ()
  49.           | copyall to (v1::vr) = 
  50.             let val len1 = length v1
  51.             in blit_ v1 0 newvec to len1; copyall (to+len1) vr end
  52.     in copyall 0 vecs; newvec end;
  53.  
  54. fun foldl f e a = 
  55.     let val stop = length a
  56.         fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  57.                        else res
  58.     in lr 0 e end
  59.  
  60. fun foldr f e a =
  61.     let fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  62.                        else res
  63.     in rl (length a - 1) e end
  64.  
  65. fun app f a = 
  66.     let val stop = length a
  67.         fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  68.                    else ()
  69.     in lr 0 end
  70.  
  71. fun map (f : elem -> elem) (a : vector) : vector = 
  72.     let val stop = length a
  73.         val newvec = vector_ stop 
  74.         fun lr j = if j < stop then (update_ newvec j (f(sub_ a j)); 
  75.                                      lr (j+1))
  76.                    else ()
  77.     in lr 0; newvec end
  78.  
  79. fun sliceend (a, i, NONE) = 
  80.         if i<0 orelse i>length a then raise Subscript
  81.         else length a
  82.   | sliceend (a, i, SOME n) = 
  83.         if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  84.         else i+n;
  85.  
  86. fun foldli f e (slice as (a, i, _)) = 
  87.     let fun loop stop =
  88.             let fun lr j res = 
  89.                 if j < stop then lr (j+1) (f(j, sub_ a j, res))
  90.                 else res
  91.             in lr i e end
  92.     in loop (sliceend slice) end;
  93.  
  94. fun foldri f e (slice as (a, i, _)) = 
  95.     let fun loop start =
  96.             let fun rl j res = 
  97.                     if j >= i then rl (j-1) (f(j, sub_ a j, res))
  98.                     else res
  99.             in rl start e end;
  100.     in loop (sliceend slice - 1) end
  101.  
  102. fun appi f (slice as (a, i, _)) = 
  103.     let fun loop stop = 
  104.             let fun lr j = 
  105.                     if j < stop then (f(j, sub_ a j); lr (j+1)) 
  106.                     else ()
  107.             in lr i end
  108.     in loop (sliceend slice) end;
  109.  
  110. fun mapi (f : int * elem -> elem) (slice as (a : vector, i, _)) : vector = 
  111.     let val stop = sliceend slice
  112.         val newvec = vector_ (stop - i)
  113.         fun loop stop = 
  114.             let fun lr j = 
  115.                     if j < stop then 
  116.                         (update_ newvec (j-i) (f(j, sub_ a j)); 
  117.                          lr (j+1)) 
  118.                     else ()
  119.             in lr i end
  120.     in loop stop; newvec end;
  121. end
  122.  
  123.